package com.cloud.fork.service;

import com.cloud.fork.domain.Context;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CombineLoadDataService extends AbstractLoadDataService implements ApplicationContextAware {

    List<AbstractLoadDataService> serviceList = new ArrayList<>();

    @Override
    public void load(Context context) {
        for (AbstractLoadDataService abstractLoadDataService : serviceList) {
            abstractLoadDataService.setContext(context);
            abstractLoadDataService.fork();
        }
    }

    @Override
    public Context getContext() {
        serviceList.forEach(AbstractLoadDataService::join);
        return super.getContext();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        serviceList.add(applicationContext.getBean(PhoneLoadDataService.class));
        serviceList.add(applicationContext.getBean(UserLoadDataService.class));
    }
}
